<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="file/upload">
    <input type="file" name="file" />
    <input type="submit" value="上传"/>
    <a href="/file/download?fileName=文件名">文件下载</a>
</form>
</br>

<input type="file" id="file2" value="ajax">
<input type="button" value="ajax上传" onclick="uploadFile()"/>
<a href="#" onclick="demodownload()">ajax下载</a>
<a href="#" onclick="deleteFile()">删除文件</a>

</br>
<a href="#" onclick="exportExcel()">导出execl</a>

</br>
<input type="file" id="file3" value="ajax">
<input type="button" value="导入execl" onclick="importExcel()"/>

</body>
</html>
<script th:src="@{/js/jquery.min.js}"></script>
<script>
    var filename = "";
    function uploadFile() {
        var file=$("#file2")[0].files[0];
        var formData = new FormData();
        formData.append("file",file);
        $.ajax({
            type:"post",
            url:"file/ajaxUpload",
            processData: false,
            contentType:false,
            data:formData,
            success:function (filepath) {
                filename = filepath;
                console.log(filepath);
            }
        })
    }
    function importExcel() {
        var file=$("#file3")[0].files[0];
        var formData = new FormData();
        formData.append("file",file);
        $.ajax({
            type:"post",
            url:"file/importExcel",
            processData: false,
            contentType:false,
            data:formData,
            success:function (result) {
                alert(result)
            }
        })
    }

    function demodownload(){
        const link = document.createElement('a');
        link.href = "file/download?filename="+filename;
        link.click()
    }
    function exportExcel(){
        const link = document.createElement('a');
        link.href = "file/exportExcel";
        link.click()
    }

    function deleteFile(){
        $.ajax({
            type:"get",
            url:"file/deleteFile?filename="+filename,
            dataType : "json",
            success:function (result) {
                alert(result)
            }
        })
    }

</script>